﻿using Servo_Tool.entity;
using Servo_Tool.handler;
using Servo_Tool.cache;
using Servo_Tool.client;
using Servo_Tool.dal;
using Servo_Tool.utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Servo_Tool.init
{
    public class StartServo
    {
        private readonly string unitNo;
        public event Action<string, string, string> ShowMessageUpdated;
        //监听
        private readonly ListenHandler listenHandler;

        public StartServo(string unitNo, ListenHandler listenHandler)
        {
            this.unitNo = unitNo;
            this.listenHandler = listenHandler;
        }
        public void LoadUnit()
        {
            string itemSql = $"SELECT * FROM PLC_ITEM WHERE UNIT_NO='{unitNo}'";
            string memSql = $"SELECT * FROM MEM_ITEM WHERE UNIT_NO='{unitNo}'";
            string readSql = $"SELECT * FROM PLC_READ WHERE UNIT_NO='{unitNo}'";
            string scanSql = $"SELECT * FROM PLC_SCAN_EVENT WHERE UNIT_NO='{unitNo}'";
            List<PlcItemEntity> itemByUnit = DBFactory.QueryList<PlcItemEntity>(itemSql);
            List<MemItemEntity> memByUnit = DBFactory.QueryList<MemItemEntity>(memSql);
            List<PlcReadEntity> readByUnit = DBFactory.QueryList<PlcReadEntity>(readSql);
            List<ScanEventEntity> scanByUnit = DBFactory.QueryList<ScanEventEntity>(scanSql);

            if (itemByUnit.Count > 0)
            {
                ShowMessage("0", $"设备[{unitNo}]加载[PLC_ITEM]数量[{itemByUnit.Count}]");
                GlobalCachePool.plcItems_dict.Add(unitNo, itemByUnit);
                foreach (PlcItemEntity item in itemByUnit)
                {
                    ServoUtils.InitPlcItem(item);
                }
            }

            if (memByUnit.Count > 0)
            {
                ShowMessage("0", $"设备[{unitNo}]加载[MEM_ITEM]数量[{memByUnit.Count}]");
                GlobalCachePool.memItems_dict.Add(unitNo, memByUnit);
                foreach (MemItemEntity item in memByUnit)
                {
                    ServoUtils.InitMemItem(item);
                }
            }

            if (scanByUnit.Count > 0)
            {
                ShowMessage("0", $"设备[{unitNo}]加载[SCAN_EVENT]数量[{scanByUnit.Count}]");
                GlobalCachePool.scanEvents_dict.Add(unitNo, scanByUnit);
                foreach (ScanEventEntity eva in scanByUnit)
                {
                    ServoUtils.InitScanEvent(eva);
                }
            }
            if (readByUnit.Count > 0)
            {
                ShowMessage("0", $"设备[{unitNo}]加载[PLC_READ]数量[{readByUnit.Count}]");
                GlobalCachePool.plcReads_dict.Add(unitNo, readByUnit);
                foreach (PlcReadEntity read in readByUnit)
                {
                    ServoUtils.InitPlcRead(read);
                }
            }
            //开启设备连接(根据设备的PLC_NO建立不同的设备连接)
            if (GlobalCachePool.single_unit_dict.TryGetValue(unitNo, out PlcUnitEntity unit))
            {
                //ModBus协议则启动ModBus客户端
                //ModbusClient modbusClient = new ModbusClient(unitNo, unit.HOST, unit.PORT);
                //GlobalCachePool.client_dict.Add(unit.UNIT_NO, modbusClient);
                //modbusClient.Start();
                
                //需要启动S7协议
            }
            //数据装载完成后开启监听
            //listenHandler.StartListening();
        }

        public void ShowMessage(string code, string msg)
        {
            ShowMessageUpdated?.Invoke(unitNo, code, msg);
        }
    }
}
